//
//  SchoolSelectView.m
//  Comma
//
//  Created by 刘轩博 on 15/1/17.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "CollegeRegisterView.h"
#import "CollegeTableViewCell.h"

@implementation CollegeRegisterView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/


- (instancetype)initWithExitImage:(NSString *)exitImg collegeText:(NSString *)collegeText
{
    self = [super initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    if (self) {
        if (exitImg) {
            [self createExitButtonWithImage:exitImg];
        }
        if (collegeText) {
            [self createCollegeFieldWithText:collegeText];
        }
    }
    return self;
}

#pragma mark - 创建控件
- (void)createExitButtonWithImage:(NSString *)image
{
    self.backgroundColor = [UIColor whiteColor];
    
    self.exitButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _exitButton.frame = CGRectMake(16, 30, 18, 18);
    [_exitButton setBackgroundImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
    
    [self addSubview:_exitButton];
}

- (void)createCollegeFieldWithText:(NSString *)text
{
    self.collegeField = [[UITextField alloc] initWithFrame:CGRectMake(16, 64, SCREEN_WIDTH - 16, 44)];
    _collegeField.placeholder = text;
    [_collegeField setFont:[UIFont fontWithName:@"Helvetica-Bold" size:14.0]];
    [_collegeField setTextColor:TEXTERGRAY];
    [self addSubview:_collegeField];
    
    // 分割线
    [GlobalMethod drawLineWithStartPoint:CGPointMake(0, 108) EndPoint:CGPointMake(SCREEN_WIDTH, 108) inUIView:self];
}

// 回收键盘
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self endEditing:YES];
}

#pragma mark - 创建tableView 显示搜索出来的大学名称
- (void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(16, 110, SCREEN_WIDTH - 16, SCREEN_HEIGHT - 110) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[CollegeTableViewCell class] forCellReuseIdentifier:@"collegeCell"];
    _tableView.bounces = NO;
    
    _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    
    [self addSubview:_tableView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_tViewArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CollegeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"collegeCell"];
    // 设置cell的线消失
    cell.separatorInset = UIEdgeInsetsMake(0, 375, 0, 0);
    
    NSDictionary *collegeDic = [_tViewArray objectAtIndex:indexPath.row];
    NSString *name = [collegeDic objectForKey:@"name"];
    [cell.collegeName setText:name];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 33.0f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *dic = [_tViewArray objectAtIndex:indexPath.row];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"COLLEGESELECTED" object:self userInfo:dic];
}


@end
